package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Modelo.Cenario;
import br.com.tiespec.model.Util.DAOException;

@Stateless(mappedName = "CenarioDAO")
public class CenarioDAO extends DAO {

	private static final long serialVersionUID = 1L;


	public Cenario selecionar(Long id) {
		Cenario cbInstance = new Cenario();
		try {
			begin();
			cbInstance = (Cenario) this.session.createCriteria(Cenario.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}

	public void remover(Long id) throws DAOException {
		delete(Cenario.class, id);
	}
	
	public Cenario selecionarComFluxos(Long id) {
		Cenario cbInstance = new Cenario();
		try {
			begin();
			cbInstance = (Cenario) this.session.createCriteria(Cenario.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("fluxos", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}

	@SuppressWarnings("unchecked")
	public List<Cenario> listarCenarios(Long idDescricaoEstendida) {
		List<Cenario> list = new ArrayList<Cenario>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(Cenario.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("descricaoEstendida.id", idDescricaoEstendida));
			list = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return list;	}

}